<?php
class ModelConfigPosition extends Model {
	public function addPosition($data) {
		$sql = "INSERT INTO `" . DB_PREFIX . "config_position` SET ".
		"position_name = '" . $this->db->escape($data['position_name']) . "', ".
		"charge_person = '" . $this->db->escape($data['charge_person']) . "', ".
		"phone_num = '" . $this->db->escape($data['phone_num']) . "', ".
		"position_left_up_x = '" . $this->db->escape($data['position_left_up_x']) .  "',".
		"position_left_up_y = '" . $this->db->escape($data['position_left_up_y']) ."',".
		"position_left_down_x = '" . $this->db->escape($data['position_left_down_x']) . "',".
		"position_left_down_y = '" . $this->db->escape($data['position_left_down_y']) . "',".
		"position_right_down_x = '" . $this->db->escape($data['position_right_down_x']) . "',".
		"position_right_down_y = '" . $this->db->escape($data['position_right_down_y']) . "',".
		"position_right_up_x = '" . $this->db->escape($data['position_right_up_x']) . "',".
		"position_right_up_y = '" . $this->db->escape($data['position_right_up_y']) . "'";
		$this->db->query($sql);
	}
	
	public function editPosition($position_id_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "config_position` SET ".
		"position_name = '" . $this->db->escape($data['position_name']) . "', ".
		"charge_person = '" . $this->db->escape($data['charge_person']) . "', ".
		"phone_num = '" . $this->db->escape($data['phone_num']) . "', ".
		"position_left_up_x = '" . $this->db->escape($data['position_left_up_x']) .  "',".
		"position_left_up_y = '" . $this->db->escape($data['position_left_up_y']) ."',".
		"position_left_down_x = '" . $this->db->escape($data['position_left_down_x']) . "',".
		"position_left_down_y = '" . $this->db->escape($data['position_left_down_y']) . "',".
		"position_right_down_x = '" . $this->db->escape($data['position_right_down_x']) . "',".
		"position_right_down_y = '" . $this->db->escape($data['position_right_down_y']) . "',".
		"position_right_up_x = '" . $this->db->escape($data['position_right_up_x']) . "',".
		"position_right_up_y = '" . $this->db->escape($data['position_right_up_y']) . "' WHERE position_id = '" . (int)$position_id_id . "'");
	}
	
	
	public function getTotalPositions() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_position`");
		return $query->row['total'];
	}
	
	public function getPosition($position_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "config_position` WHERE position_id = '" . (int)$position_id . "'");
		return $query->row;
	}
	
	public function deletePosition($position_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "config_position` WHERE position_id = '" . (int)$position_id . "'");
	}
	
	public function getPositions($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_position`";
			
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	
	public function getPositionByAxis($x,$y){
		$sql = "";
	} 
}
?>